package org.jeecg.modules.message.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 消息定时任务配置类
 * 用于配置定时任务线程池
 */
@Configuration
@EnableScheduling
public class MessageSchedulingConfig {

    /**
     * 配置定时任务线程池
     * @return ThreadPoolTaskScheduler
     */
    @Bean(name = "messageScheduler")
    public ThreadPoolTaskScheduler messageScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        // 设置线程池大小
        scheduler.setPoolSize(5);
        // 设置线程名称前缀
        scheduler.setThreadNamePrefix("message-scheduler-");
        // 线程池满时的拒绝策略
        scheduler.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 是否等待所有任务完成后再关闭线程池
        scheduler.setWaitForTasksToCompleteOnShutdown(true);
        // 等待任务完成的最大时间（秒）
        scheduler.setAwaitTerminationSeconds(60);
        return scheduler;
    }
}
